package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.config.sign.util.NoSslHttpUtils;
import org.jeecg.config.vo.HttpResult;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarningCount;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarningHistory;
import org.jeecg.modules.monitoring.j_monitoring.mapper.GatewayWarningHistoryMapper;

import org.jeecg.modules.monitoring.j_monitoring.service.IGatewayWarningHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: gateway_warning_history
 * @Author: xiaoyq
 * @Date:   2023-05-09
 * @Version: V1.0
 */
@Service
public class GatewayWarningHistoryServiceImpl extends ServiceImpl<GatewayWarningHistoryMapper, GatewayWarningHistory> implements IGatewayWarningHistoryService {

    @Autowired
    private Environment env;

    private String warningList = "/ng/alarm/getHistoryAlarms";

    public void syncGatewayWarningHistory(){
        String url = env.getProperty("gatewaySystem.url");

        String header = env.getProperty("gatewaySystem.header");

        try {
            HttpResult res = NoSslHttpUtils.noSslGet(url+warningList,header);

            List<GatewayWarningHistory> list = JSONArray.parseArray(res.getBody(), GatewayWarningHistory.class);

            QueryWrapper<GatewayWarningHistory> queryWrapper = new QueryWrapper<GatewayWarningHistory>();
            List<GatewayWarningHistory> oldList = baseMapper.selectList(queryWrapper);

            List<String> deleteIds = this.getDeleteIds(list,oldList);

            /**
             * 删除不同项
             */
            if (deleteIds.size()>=1){
                baseMapper.deleteBatchIds(deleteIds);
            }

            for (GatewayWarningHistory warning : list){

                if (this.canInsert(oldList,warning.getId())){
                    baseMapper.insert(warning);
                }else{
                    baseMapper.updateById(warning);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e);
        }

    }

    private Boolean canInsert(List<GatewayWarningHistory> warningList,String id){

        for (GatewayWarningHistory warning : warningList){

            if (warning.getId().equals(id)){
                return false;
            }

        }
        return true;
    }

    private List<String> getDeleteIds(List<GatewayWarningHistory> newList,List<GatewayWarningHistory> oldList){

        List<String> newString = new ArrayList<>();
        List<String> oldString = new ArrayList<>();
        for (GatewayWarningHistory newWarning : newList){
            newString.add(newWarning.getId());
        }
        for (GatewayWarningHistory oldWarning : oldList){
            oldString.add(oldWarning.getId());
        }
        oldString.removeAll(newString);
        return oldString;
    }


    public GatewayWarningCount getWarningCount(String fdn){
        return baseMapper.getWarningCount(fdn);
    }

    public void removeAll(){
        baseMapper.removeAll();
    }
}
